package com.extension.company.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.extension.company.admin.R;
import com.extension.company.model.entity.AddressListData;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 收货地址的适配器
 */
public class AddressListAdapter extends BaseRecyclerAdapter<AddressListData.DataBean.AddressBean, AddressListAdapter.Holder> {

	public AddressListAdapter(Context context, List<AddressListData.DataBean.AddressBean> list) {
		super(context, list);
	}

	@Override
	public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
		View view = mInflater.inflate(R.layout.addresslist_item, parent, false);
		Holder holder = new Holder(view);
		return holder;
	}

	@Override
	public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {

		Holder holder = (Holder) viewHolder;  // 前面人封装的，需要强转

		holder.addressUsername.setText(list.get(position).getName());

		holder.addressPhone.setText(list.get(position).getPhone());

		holder.addressName.setText(list.get(position).getPname() + list.get(position).getCname()
				+ list.get(position).getRname() + list.get(position).getDetail());

		holder.checkBox.setChecked("1".equals(list.get(position).getIs_default()) ? true : false);

		holder.addressDel.setOnClickListener(new View.OnClickListener() { // 删除的点击事件
			@Override
			public void onClick(View v) {
				mDelOnclickListener.delItem(list.get(position).getId(), position);
			}
		});

		holder.addressEdit.setOnClickListener(new View.OnClickListener() {  // 编辑收货地址
			@Override
			public void onClick(View v) {
				mDelOnclickListener.editItem(list.get(position));
			}
		});
		holder.itemView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mDelOnclickListener.click(list.get(position));
			}
		});
		holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					mDelOnclickListener.defaultAddressCheck(list, position, true);
				} else {
					mDelOnclickListener.defaultAddressCheck(list, position, false);

				}
			}
		});

	}

	class Holder extends RecyclerView.ViewHolder {
		@BindView(R.id.address_username)
		TextView addressUsername;
		@BindView(R.id.address_phone)
		TextView addressPhone;
		@BindView(R.id.address_name)
		TextView addressName;
		@BindView(R.id.address_edit)
		TextView addressEdit;
		@BindView(R.id.address_del)
		TextView addressDel;
		@BindView(R.id.address_check)
		CheckBox checkBox;

		public Holder(View itemView) {
			super(itemView);
			ButterKnife.bind(this, itemView);
		}
	}

	public interface DelOnclickListener {
		void delItem(int delDataId, int position); // 删除

		void editItem(AddressListData.DataBean.AddressBean addressBean); // 编辑

		void click(AddressListData.DataBean.AddressBean addressBean);

		void defaultAddressCheck(List<AddressListData.DataBean.AddressBean> list, int defaultAddressPosition, boolean isDefaultAddress);
	}

	private DelOnclickListener mDelOnclickListener;

	public void setDelClickListener(DelOnclickListener mDelOnclickListener) {
		this.mDelOnclickListener = mDelOnclickListener;
	}


}
